package fgoScript.service;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

import commons.entity.Constant;
import fgoScript.constant.GameConstant;
import fgoScript.entity.PointColor;
import commons.util.GameUtil;
import fgoScript.exception.AppNeedRestartException;
import fgoScript.exception.AppNeedStopException;

public abstract class AutoAct {
	// 点色判断集合
	private List<PointColor> pcList;
	// 结束点色
	private List<PointColor> finishPCList;
	// 结束点色
	private List<PointColor> beforeFinishPCList;
	// 当前点色
	private PointColor pcWait;
	
	private boolean GO_FLAG;

	public List<PointColor> getBeforeFinishPCList() {
		return beforeFinishPCList;
	}

	public void setBeforeFinishPCList(List<PointColor> beforeFinishPCList) {
		this.beforeFinishPCList = beforeFinishPCList;
	}

	public PointColor getPcWait() {
		return pcWait;
	}
	

	public boolean isGO_FLAG() {
		return GO_FLAG;
	}


	public void setGO_FLAG(boolean gO_FLAG) {
		GO_FLAG = gO_FLAG;
	}


	public void setPcWait(PointColor pcWait) {
		this.pcWait = pcWait;
	}
	public List<PointColor> getPcList() {
		return pcList;
	}
	public void setPcList(List<PointColor> pcList) {
		this.pcList = pcList;
	}

	public List<PointColor> getFinishPCList() {
		return finishPCList;
	}
	public void setFinishPCList(List<PointColor> finishPCList) {
		this.finishPCList = finishPCList;
	}

	public AutoAct(List<PointColor> pcList, List<PointColor> finishPCList) {
		super();
		pcList.addAll(finishPCList);
		this.pcList = pcList;
		this.finishPCList = finishPCList;
	}
	public AutoAct(List<PointColor> pcList, List<PointColor> finishPCList, List<PointColor> beforeFinishPCList) {
		super();
		pcList.addAll(finishPCList);
		this.pcList = pcList;
		this.finishPCList = finishPCList;
		this.beforeFinishPCList = beforeFinishPCList;
	}
	/**
	 * 自动情景行动累
	 * @throws Exception
	 * @throws AWTException 
	 */
	public boolean autoAct() throws Exception {
		boolean match=false;
		boolean bMatch=false;
		int size = pcList.size() * 10;
		int fSize = 0;
		if(null != beforeFinishPCList){
			fSize = beforeFinishPCList.size();
		}
		int countFSize = fSize;
		Point clickPoint;
		for (int i = 0; i < size; i++) {
			pcWait = GameUtil.waitUntilOneColor(pcList, Constant.FGOMonitor);
			if (pcWait!=null) {
				doSomeThing();
				//
				doClickFromPcList(pcWait);

				PointColor pcTemp;
				PointColor bfPcTemp;
				Color tempC;
				for (int j = 0; j < finishPCList.size(); j++) {
					pcTemp = finishPCList.get(j);
					match = GameUtil.likeEqualColor(pcWait.getColor(), pcTemp.getColor());
					if (match) {
						//
						if(null != beforeFinishPCList){
							for (int k = 0; k < fSize; k++) {
								bfPcTemp = beforeFinishPCList.get(k);
								tempC = GameUtil.getScreenPixel(bfPcTemp.getPoint());

								bMatch = GameUtil.likeEqualColor(tempC, bfPcTemp.getColor());
								if(bMatch){
									//
									doClickFromPcList(bfPcTemp);
									break;
								}

							}
						}
						//
						return true;
					}
				}
				if (match && countFSize-- == 0) {
					break;
				}
			} else {
				return false;
			}
		
		}
		return true;
	}
	private void doClickFromPcList(PointColor pc) throws AppNeedRestartException, AppNeedStopException {
		List<Point> pointList = pc.getClickPointList();
		Point clickPoint =  pc.getClickPoint();
		int size;
		if(pointList!= null){
			size = pointList.size();
			for (int i = 0; i < size; i++) {
				clickPoint = pointList.get(i);
				if (clickPoint != null) {
					clickByPoint(pc,clickPoint);
				}
				GameUtil.delay(5000);
			}
		}else if(clickPoint != null) {
			clickByPoint(pc,clickPoint);
		}
	}
	private void clickByPoint(PointColor pc, Point pt) throws AppNeedRestartException, AppNeedStopException {
		GameUtil.getMymouse().mouseMoveByPoint(pt,true,true);
		if (pc.isConfirm()){
			GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);
		}else{
			GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
		}
	}
	public abstract void doSomeThing() throws Exception;
}
